package com.nuanshui.framework.lang;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.nuanshui.framework.utils.CommonUtil;
import org.apache.commons.lang3.StringUtils;


public class SysStringUtils extends StringUtils {

	private static String randomStr = "0123456789";
	private static final char UNDERLINE='_';

	/**
	 * 转化为大写
	 * 
	 * @param str
	 *            要被转化的字符串
	 * @param start
	 *            开始的位置
	 * @param end
	 *            结束的位置
	 * @return
	 */
	public static String toUpperCase(final String str, final int start,
			final int end) {

		if (isEmpty(str)) {
			return str;
		}
		StringBuilder sb = new StringBuilder();
		sb.append(str.substring(0, start));
		sb.append(str.substring(start, end).toUpperCase());
		sb.append(str.substring(end));
		return sb.toString();
	}

	public static boolean isNullOrEmpty(String str) {
		return str == null || str.trim().isEmpty();
	}

	/**
	 * 转化为小写
	 * 
	 * @param str
	 *            要被转化的字符串
	 * @param start
	 *            开始的位置
	 * @param end
	 *            结束的位置
	 * @return
	 */
	public static String toLowerCase(final String str, final int start,
			final int end) {

		if (isEmpty(str)) {
			return str;
		}
		StringBuilder sb = new StringBuilder();
		sb.append(str.substring(0, start));
		sb.append(str.substring(start, end).toLowerCase());
		sb.append(str.substring(end));
		return sb.toString();
	}

	public static boolean hasText(String str) {
		return hasText((CharSequence)str);
	}

	public static boolean hasText(CharSequence str) {
		if(!hasLength(str)) {
			return false;
		} else {
			int strLen = str.length();

			for(int i = 0; i < strLen; ++i) {
				if(!Character.isWhitespace(str.charAt(i))) {
					return true;
				}
			}

			return false;
		}
	}

	public static boolean hasLength(CharSequence str) {
		return str != null && str.length() > 0;
	}

	/**
	 * 获取随机字符串
	 * 
	 * @param num
	 *            个数
	 * @param str
	 *            从传入的字符串范围内获取
	 * @return
	 */
	public static String getRandomString(final int num, final String str) {
		if (isEmpty(str)) {
			throw new RuntimeException("传入的字符串不能为空");
		}
		StringBuilder sb = new StringBuilder();
		int length = str.length();
		Random random = new Random();
		for (int i = 0; i < num; i++) {
			sb.append(str.charAt(random.nextInt(length)));
		}
		return sb.toString();
	}

	public static String getRandomString(final int num) {
		StringBuilder sb = new StringBuilder();
		int length = randomStr.length();
		Random random = new Random();
		for (int i = 0; i < num; i++) {
			sb.append(randomStr.charAt(random.nextInt(length)));
		}
		return sb.toString();
	}

	/**
	 * 严格判断是否包含 就是如果两个字符串相等表示不包含
	 * 
	 * @param containStr
	 * @param containedStr
	 * @return
	 */
	public static boolean strictContain(final String containStr,
			final String containedStr) {
		if (CommonUtil.anyNull(containedStr, containedStr)) {
			return false;
		}
        return containStr != null && !containStr.equals(containedStr)
                && containStr.contains(containedStr);

    }

	/**
	 * 判断是否包含 就是如果两个字符串相等表示不包含
	 * 
	 * @param containStr
	 * @param containedStr
	 * @return
	 */
	public static boolean contain(final String containStr,
			final String containedStr) {
		if (CommonUtil.anyNull(containedStr, containedStr)) {
			return false;
		}
        return containStr != null && !containStr.equals(containedStr)
                && containStr.contains(containedStr);

    }
	
	public static String camelToUnderline(String param){  
	       if (param==null||"".equals(param.trim())){  
	           return "";  
	       }  
	       int len=param.length();  
	       StringBuilder sb=new StringBuilder(len);  
	       for (int i = 0; i < len; i++) {  
	           char c=param.charAt(i);  
	           if (Character.isUpperCase(c)){  
	               sb.append(UNDERLINE);  
	               sb.append(Character.toLowerCase(c));  
	           }else{  
	               sb.append(c);  
	           }  
	       }  
	       return sb.toString();  
	   }  
	   public static String underlineToCamel(String param){  
	       if (param==null||"".equals(param.trim())){  
	           return "";  
	       }  
	       int len=param.length();  
	       StringBuilder sb=new StringBuilder(len);  
	       for (int i = 0; i < len; i++) {  
	           char c=param.charAt(i);  
	           if (c==UNDERLINE){  
	              if (++i<len){  
	                  sb.append(Character.toUpperCase(param.charAt(i)));  
	              }  
	           }else{  
	               sb.append(c);  
	           }  
	       }  
	       return sb.toString();  
	   } 
	   
	   public static String underlineToCamel2(String param){  
	       if (param==null||"".equals(param.trim())){  
	           return "";  
	       }  
	       StringBuilder sb=new StringBuilder(param);  
	       Matcher mc= Pattern.compile("_").matcher(param);  
	       int i=0;  
	       while (mc.find()){  
	           int position=mc.end()-(i++);  
	           //String.valueOf(Character.toUpperCase(sb.charAt(position)));  
	           sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());  
	       }  
	       return sb.toString();  
	   }
	   
	  public static void main(String[] args) {
		  
	  }
	  
	  public static boolean isLogicEmpty(String value) {
		  if(isEmpty(value)) return true;
		  value = value.trim();
		  if(isEmpty(value)) return true;
          return "(null)".equals(value);

      }
	  
	  /**
	   * 剔除空格，换行符
	   * @param 
	   * @author guohao
	   * @date 2016年3月15日 下午2:34:05
	   */
	  public static String rejectBlank(String str) {      
        if(str!=null && !"".equals(str)) {      
            Pattern p = Pattern.compile("\\s*|\t|\\r|\\n|\r|r|");      
            Matcher m = p.matcher(str);      
            String strNoBlank = m.replaceAll("");
            strNoBlank = strNoBlank.replace("\\r", "").replace("\\s+", "");
            return strNoBlank;      
        }else {      
            return str;      
        }           
	 } 
	  
	  
	  
	  public static     String LongToString(Long num,int bitNum) {
			
			char[] chs = new char[bitNum];
			for(int i =0;i<chs.length;i++) {
				chs[i] = '0';
			}
			DecimalFormat df = new DecimalFormat(new String(chs));
			return df.format(num);	
	  }
	  
	  
	  public  static  String intToString(int num,int bitNum) {
			
			char[] chs = new char[bitNum];
			for(int i =0;i<chs.length;i++) {
				chs[i] = '0';
			}
			DecimalFormat df = new DecimalFormat(new String(chs));
			return df.format(num);	
	 }
	  
	  /**
	   * 用户星号加密字符串
	   * @param @param str
	   * @param @param start
	   * @param @param end
	   * @param @return
	   * @author guohao
	   * @date 2016年11月22日 下午3:42:30
	   * @since $
	   */
	  public static  String asteriskMixStr(String str,int start,int end) {
		  if(isEmpty(str)) return str;
		  
		  String subStr = str.substring(start,end);
		  int length = end-start;
		  StringBuilder sb =new StringBuilder();
		  for(int i =0;i<length;i++) {
			  sb.append("*");
		  }
		 return  str.replace(subStr, sb.toString());		  
	  }


	public static boolean isEmpty(Object str) {
		return str == null || "".equals(str);
	}

	// 对Map内所有value作utf8编码，拼接返回结果
	public static String toQueryString(Map<String, String> data) {
		StringBuilder queryString = new StringBuilder();
		for (Map.Entry<String, String> pair : data.entrySet()) {
			queryString.append(pair.getKey() + "=");
			try {
				queryString.append(URLEncoder.encode(pair.getValue(),
						"UTF-8") + "&");
			} catch (UnsupportedEncodingException e) {

			}
		}
		if (queryString.length() > 0) {
			queryString.deleteCharAt(queryString.length() - 1);
		}
		return queryString.toString();
	}
	
}
